package cc.rengu.redp.bizimpl.upmp.service.impl;/*
package cc.rengu.redp.bizimpl.upmp.service.impl;

import cc.rengu.redp.bizimpl.enums.BizDataAuditStatusEnum;
import cc.rengu.redp.bizimpl.enums.BizOprEnum;
import cc.rengu.redp.bizimpl.enums.PageOptFlagEnum;
import cc.rengu.redp.bizimpl.upmp.common.BusinessContainer;
import cc.rengu.redp.bizimpl.upmp.dao.TermEquipmentMapper;
import cc.rengu.redp.bizimpl.upmp.domain.*;
import cc.rengu.redp.bizimpl.upmp.service.*;
import cc.rengu.redp.bizimpl.upmp.vo.OperationResult;
import cc.rengu.redp.bizimpl.upmp.vo.UpmpUserInfoVO;
import cc.rengu.redp.bizimpl.utils.Common;
import cc.rengu.redp.bizimpl.utils.CommonUtils;
import cc.rengu.redp.bizimpl.utils.DateUtils;
import cc.rengu.redp.common.exception.RedpException;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

*/
/**
 * @author 刘玉亮.LL
 *//*

@Service
public class TermEquipmentServiceImpl extends ServiceImpl<TermEquipmentMapper, TermEquipment> implements ITermEquipmentService {

    @Autowired
    private TransactionTemplate transactionTemplate;
    @Autowired
    private IBizTmpService bizTmpService;
    @Autowired
    private IUpmpPrivilegeCheckService authPrivilegeCheckService;
    @Autowired
    private IUpmpSysParamService upmpSysParamService;
    @Autowired
    private IUpmpOrganizationService organizationService;
    @Autowired
    private DeviceActionService deviceActionService;
    @Autowired
    private TermBrandNewService termBrandNewService;
    @Autowired
    private TermModelNewService termModelNewService;

    public OperationResult saveData(String oprMenuId, String oprMenuName, TermEquipment entity, HttpServletRequest request, String webNextUrl) throws RedpException {
        String flag = request.getParameter("optFlag");
        String id = request.getParameter("id");
        java.util.Map<String, Object> result = com.google.common.collect.Maps.newHashMap();
        String lastUpddt = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
         UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
        UpmpOrganization org = organizationService.getOrgByCode(user.getOrgCode(), user.getInstId());
        TermEquipment oldEntity = null;
        if (!PageOptFlagEnum.OPT_FLAG_ADD.getType().equals(flag)){
            oldEntity = this.getById(id);
        }
        //00-未使用；01-已使用；02-损毁；03-故障；04-装机出库
        if (PageOptFlagEnum.OPT_FLAG_ADD.getType().equals(flag)) {
            String deviceNo = getAutoDeviceNo(DateUtils.formatDate(DateUtils.currentDate(), DateUtils.FORMAT_YYYYMM));
            UpmpPrivilegeCheck privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), "终端库存管理:新增");
            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                entity.setId(null);
                entity.setDeviceNo(deviceNo);
                //         entity.setInstId(user.getInstId());
                entity.setOprId(user.getId());
                //         entity.setCrtOrgId(org.getOrgCode());
                entity.setCreateTime(lastUpddt);
                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType());
                this.save(entity);
                bizTmpService.saveBizTmp(entity, user, org.getInstId(), BizOprEnum.OPR_ADD.getType(), lastUpddt
                        , oprMenuId, oprMenuName, entity.getStatus(), entity.getAuditStatus(), String.valueOf(entity.getId()), webNextUrl,entity.getMchntNo(),null,entity.getTermNo());
                result.put("id", entity.getId());
            } else {
                entity.setId(null);
                entity.setDeviceNo(deviceNo);
                //    entity.setInstId(user.getInstId());
                entity.setOprId(user.getId());
                //     entity.setCrtOrgId(org.getOrgCode());
                entity.setCreateTime(lastUpddt);
                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                this.save(entity);

                bizHisService.saveBizHisNew(entity,entity, user, org.getInstId(), BizOprEnum.OPR_ADD.getType(), lastUpddt
                        , oprMenuId, oprMenuName, String.valueOf(entity.getId()),entity.getMchntNo(),null,entity.getTermNo());
//                bizTmpService.saveBizHis(entity, user, org.getInstId(), BizOprEnum.OPR_ADD.getType(), lastUpddt
//                        , oprMenuId, oprMenuName, String.valueOf(entity.getId()),entity.getMchntNo(),null,entity.getTermNo());
                result.put("id", entity.getId());
            }
        } else if (PageOptFlagEnum.OPT_FLAG_UPDATE.getType().equals(flag)) {
            if (Common.isBlank(id)) {
                 log.error("请求参数(id)异常!");
                return OperationResult.buildFailureResult("请求参数(id)异常!", result);
            }
            UpmpPrivilegeCheck privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), "终端库存管理:修改");
            entity.setLastOprId(user.getId());
            entity.setLastUpdateTime(lastUpddt);
            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                entity.setLastOprId(user.getId());
                entity.setLastUpdateTime(lastUpddt);
                entity.setReserve6("");
                this.updateAuditStatus(id, entity.getAuditStatus());//只修改状态
                bizTmpService.saveBizTmp(entity, user, org.getInstId(), BizOprEnum.OPR_UPDATE.getType(), lastUpddt
                        , oprMenuId, oprMenuName, entity.getStatus(), entity.getAuditStatus(), String.valueOf(entity.getId()), webNextUrl,entity.getMchntNo(),null,entity.getTermNo());
            } else {
                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                entity.setReserve6("");
                TermEquipment termEquipment = this.getById(id);
                BeanUtils.copyProperties(entity, termEquipment);
                this.save(termEquipment);
                //历史表新增
                bizHisService.saveBizHisNew(termEquipment,oldEntity, user, org.getInstId(), BizOprEnum.OPR_UPDATE.getType(), lastUpddt
                        , oprMenuId, oprMenuName, String.valueOf(termEquipment.getId()),termEquipment.getMchntNo(),null,termEquipment.getTermNo());
//                bizTmpService.saveBizHis(termEquipment, user, org.getInstId(), BizOprEnum.OPR_UPDATE.getType(), lastUpddt
//                        , oprMenuId, oprMenuName, String.valueOf(termEquipment.getId()),termEquipment.getMchntNo(),null,termEquipment.getTermNo());
            }
        } else if (PageOptFlagEnum.OPT_FLAG_CHECK.getType().equals(flag)) {
            if (Common.isBlank(id)) {
                 log.error("请求参数(id)异常!");
                return OperationResult.buildFailureResult("请求参数(id)异常!", result);
            }
            //获取临时表数据并转换成实体对象
            BizTmp tmp = bizTmpService.getById(id);
            if (Common.isBlank(tmp)) {
                 log.error("根据id没有找到指定临时表数据！");
                return OperationResult.buildFailureResult("根据id没有找到指定临时表数据！", result);
            }
            String bizdata = "";
            if (null != tmp.getBizData1()) {
                bizdata += tmp.getBizData1();
            }
            if (null != tmp.getBizData2()) {
                bizdata += tmp.getBizData2();
            }
            if (null != tmp.getBizData3()) {
                bizdata += tmp.getBizData3();
            }

            TermEquipment termEquipmentNew = JSON.parseObject(bizdata, TermEquipment.class);

            TermEquipment termEquipment = this.getById(id);
            // 备份正式表数据
            TermEquipment termEquipmentOld = new TermEquipment();
            BeanUtils.copyProperties(termEquipment, termEquipmentOld);
            if (Common.isBlank(termEquipment) && !BizOprEnum.OPR_ADD.equals(tmp.getBizOpr())) {
                 log.error("根据id没有找到指定数据！");
                return OperationResult.buildFailureResult("根据id没有找到指定数据！", result);
            }

            String opt = request.getParameter("opt");
            if (Common.isBlank(opt)) {
                 log.error("请求参数(opt)异常!");
                return OperationResult.buildFailureResult("请求参数(opt)异常!", result);
            }
            String bizOpr = "";
            if (BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType().equals(termEquipment.getAuditStatus())) {
                if (!PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {

                } else {
                    BeanUtils.copyProperties(termEquipmentNew, termEquipment);
                }
            } else {
                BeanUtils.copyProperties(termEquipmentNew, termEquipment);
            }

            String auditRefuseReason = request.getParameter("auditRefuseReason");
            if (BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType().equals(termEquipment.getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    termEquipment.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                } else {
                    termEquipment.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_REJECT.getType());
                }
                bizOpr = BizOprEnum.OPR_ADD.getType();
            } else if (BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType().equals(termEquipment.getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    termEquipment.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                } else {
                    termEquipment.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_REJECT.getType());
                }
                bizOpr = BizOprEnum.OPR_UPDATE.getType();
            }
            termEquipment.setAuditRefuseReason(auditRefuseReason);
            this.save(termEquipment);
            if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                //审批通过将正式表更新到历史表
                bizHisService.saveBizHisNew(termEquipment,termEquipmentOld, user, org.getInstId(), bizOpr, lastUpddt, oprMenuId, oprMenuName, String.valueOf(termEquipment.getId()),termEquipment.getMchntNo(),null,termEquipment.getTermNo());
//                bizTmpService.saveBizHis(termEquipment, user, org.getInstId(), bizOpr, lastUpddt, oprMenuId, oprMenuName, String.valueOf(termEquipment.getId()),termEquipment.getMchntNo(),null,termEquipment.getTermNo());
            }
            // 修改临时表的审核状态等字段
            bizTmpService.saveBizTmp(termEquipment, user, org.getInstId(), bizOpr, lastUpddt, oprMenuId, oprMenuName, termEquipment.getStatus(), termEquipment.getAuditStatus(), String.valueOf(termEquipment.getId()), webNextUrl,entity.getMchntNo(),null,entity.getTermNo());
        }

        return OperationResult.buildSuccessResult("数据保存处理完成", result);
    }

    public OperationResult saveData1(String oprMenuId, String oprMenuName, TermEquipment entity1, UpmpUserInfoVO user, UpmpOrganization org, IUpmpPrivilegeCheckService authPrivilegeCheckService, HttpServletRequest request, String webNextUrl) throws RedpException {
        String flag = request.getParameter("optFlag");
        java.util.Map<String, Object> result = com.google.common.collect.Maps.newHashMap();
        String lastUpddt = DateUtils.formatDate(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
        DeviceAction deviceAction = null;
        String bizOpr = "";
        TermEquipment entity = null;
        UpmpPrivilegeCheck privilegeCheck = new UpmpPrivilegeCheck();
        if (!PageOptFlagEnum.OPT_FLAG_CHECK.getType().equals(flag)) {
            String ids = request.getParameter("ids");
            if (Common.isBlank(ids)) {
                 log.error("请求参数(id)异常!");
                //     return "f";
                return OperationResult.buildSuccessResult("f", result);
            }
            String[] idArr = ids.split(",");
            if (idArr.length >= 1) {
                List<TermEquipment> termEquipment = new ArrayList<TermEquipment>();
                for (int i = 0; i < idArr.length; i++) {
                    TermEquipment e = this.getById(idArr[i]);
                    termEquipment.add(e);
                }

                Iterator<TermEquipment> it = termEquipment.iterator();
                while (it.hasNext()) {
                    entity = it.next();
                    if (Common.isBlank(entity)) {
                         log.error("根据id没有找到指定数据！");
                        //       return "f";
                        return OperationResult.buildSuccessResult("f", result);
                    }
                    TermEquipment oldEntity = new TermEquipment();
                    if (!PageOptFlagEnum.OPT_FLAG_ADD.getType().equals(flag)){
                        BeanUtils.copyProperties(entity,oldEntity);
                    }
                    TermEquipment entityTmp = new TermEquipment();
                    BeanUtils.copyProperties(entity,entityTmp);
                    if ("equout".equals(flag) || "repout".equals(flag) || "cheji".equals(flag)) {
                        deviceAction = new DeviceAction();
                        deviceAction.setId(DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS"));
                        deviceAction.setDeviceNo(entity.getDeviceNo());
                        deviceAction.setBrandCd(entity.getBrandCode());
                        deviceAction.setModelCd(entity.getModelCode());
                        TermModelInfo termModel = termModelNewService.findTermModel(entity.getModelCode());
                        if (termModel == null) {
                            deviceAction.setModelName("");
                        } else {
                            deviceAction.setModelName(termModel.getModelName());
                        }
                        TermBrandInfo termBrand = termBrandNewService.findTermBrand(entity.getBrandCode());
                        if (termBrand == null) {
                            deviceAction.setBrandName("");
                        } else {
                            deviceAction.setBrandName(termBrand.getBrandName());
                        }
                        deviceAction.setSn(entity.getSn());
                        deviceAction.setOprId(user.getId());//登记操作人
                        deviceAction.setCrtOrgId(entity.getCrtOrgId());
                        deviceAction.setCreateTime(lastUpddt);
                        deviceAction.setLastUpdateTime(lastUpddt);
                        deviceAction.setLastOprId(user.getId());
                        deviceAction.setStatus("00");
                        deviceAction.setLeaseStartDate(entity.getLeaseStartDate());
                        deviceAction.setLeaseEndDate(entity.getLeaseEndDate());
                        deviceAction.setMchntNo(entity.getMchntNo());
                        if ("equout".equals(flag)) {//装机出库
                            bizOpr = "0";
                            privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), "终端库存管理:装机出库");
                            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                                entity.setReserve6(flag);
                                entityTmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                                entityTmp.setReserve6(flag);
                                entityTmp.setStatus("14");
                                deviceAction = null;
                            } else {
                                entity.setStatus("14");
                                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                                deviceAction.setAction("40");
                            }
                        } else if ("repout".equals(flag)) {//维修出库
                            bizOpr = "1";
                            privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), "终端库存管理:维修出库");
                            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                                entity.setReserve6(flag);
                                entityTmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                                entityTmp.setReserve6(flag);
                                entityTmp.setStatus("13");
                                deviceAction = null;
                            } else {
                                entity.setStatus("13");
                                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                                deviceAction.setAction("30");
                            }
                        } else if ("cheji".equals(flag)) {//撤机
                            bizOpr = "2";
                            privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), "终端库存管理:撤机");
                            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                                entity.setReserve6(flag);
                                entityTmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                                entityTmp.setReserve6(flag);
                                entityTmp.setStatus("15");
                                deviceAction = null;
                            } else {
                                entity.setStatus("15");
                                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                                deviceAction.setAction("51");
                            }
                        }
                    } else if ("orgin".equals(flag) || "repin".equals(flag) || "chejiin".equals(flag)) {
                        if ("orgin".equals(flag)) {//返还入库
                            deviceAction = deviceActionService.query(entity.getDeviceNo(), "40");
                            if (deviceAction == null) {
                                //       return "f";
                                return OperationResult.buildSuccessResult("f", result);
                            }
                            bizOpr = "3";
                            privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), "终端库存管理:返还入库");
                            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                                entity.setReserve6(flag);
                                entityTmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                                entityTmp.setReserve6(flag);
                                entityTmp.setStatus("10");
                                entityTmp.setMchntNo(null);
                                entityTmp.setTermNo(null);
                                deviceAction = null;
                            } else {
                                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                                entity.setStatus("10");
                                entity.setMchntNo(null);
                                entity.setTermNo(null);
                                deviceAction.setAction("42");//返还入库 修改状态
                                deviceAction.setLastUpdateTime(lastUpddt);
                                deviceAction.setLastOprId(user.getId());
                            }
                        } else if ("repin".equals(flag)) {//修复入库
                            deviceAction = deviceActionService.query(entity.getDeviceNo(), "30");
                            if (deviceAction == null) {
                                //      return "f";
                                return OperationResult.buildSuccessResult("f", result);
                            }
                            bizOpr = "4";
                            privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), "终端库存管理:修复入库");
                            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                                entity.setReserve6(flag);
                                entityTmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                                entityTmp.setReserve6(flag);
                                entityTmp.setStatus("10");
                                entityTmp.setMchntNo(null);
                                entityTmp.setTermNo(null);
                                deviceAction = null;
                            } else {
                                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                                entity.setStatus("10");
                                entity.setMchntNo(null);
                                entity.setTermNo(null);
                                deviceAction = deviceActionService.query(entity.getDeviceNo(), "30");
                                deviceAction.setAction("31");//修复入库 修改状态
                                deviceAction.setLastUpdateTime(lastUpddt);
                                deviceAction.setLastOprId(user.getId());
                            }
                        } else if ("chejiin".equals(flag)) {//撤机入库
                            deviceAction = deviceActionService.query(entity.getDeviceNo(), "51");
                            if (deviceAction == null) {
                                //      return "f";
                                return OperationResult.buildSuccessResult("f", result);
                            }
                            bizOpr = "5";
                            privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), "终端库存管理:撤机入库");
                            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                                entity.setReserve6(flag);
                                entityTmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                                entityTmp.setReserve6(flag);
                                entityTmp.setStatus("10");
                                entityTmp.setMchntNo(null);
                                entityTmp.setTermNo(null);
                                deviceAction = null;
                            } else {
                                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                                entity.setStatus("10");
                                entity.setMchntNo(null);
                                entity.setTermNo(null);
                                deviceAction.setAction("52");//修复入库 修改状态
                                deviceAction.setLastUpdateTime(lastUpddt);
                                deviceAction.setLastOprId(user.getId());
                            }
                        }
                    } else if ("warning".equals(flag) || "sunhui".equals(flag) || "loss".equals(flag) || "give".equals(flag)) {
                        if ("warning".equals(flag)) {//报障
                            bizOpr = "6";
                            privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), "终端库存管理:报障");
                            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                                entity.setReserve6(flag);
                                entityTmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                                entityTmp.setReserve6(flag);
                                entityTmp.setStatus("13");
                                deviceAction = null;
                            } else {
                                entity.setStatus("13");
                                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                            }
                        } else if ("sunhui".equals(flag)) {//报损
                            bizOpr = "7";
                            privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), "终端库存管理:损毁");
                            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                                entity.setReserve6(flag);
                                entityTmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                                entityTmp.setReserve6(flag);
                                entityTmp.setStatus("12");
                                deviceAction = null;
                            } else {
                                entity.setStatus("12");
                                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                            }
                        } else if ("loss".equals(flag)) {//丢失
                            bizOpr = "8";
                            privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), "终端库存管理:丢失");
                            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                                entity.setReserve6(flag);
                                entityTmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                                entityTmp.setReserve6(flag);
                                entityTmp.setStatus("16");
                                deviceAction = null;
                            } else {
                                entity.setStatus("16");
                                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                            }
                        } else if ("give".equals(flag)) {//返还第三方
                            bizOpr = "9";
                            privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), "终端库存管理:返还第三方");
                            if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                                entity.setReserve6(flag);
                                entityTmp.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType());
                                entityTmp.setReserve6(flag);
                                entityTmp.setStatus("17");
                                deviceAction = null;
                            } else {
                                entity.setStatus("17");
                                entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                            }
                        }
                    }
                    if (deviceAction != null) {//终端设备在装机、换机、撤机、维修等操作过程中都会登记操作记录
                        deviceAction.setInstId(entity.getInstId());
                        deviceActionService.save(deviceAction);
                    }
                    this.save(entity);
                    if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                        bizTmpService.saveBizTmp(entityTmp, user, org.getInstId(), bizOpr, lastUpddt
                                , oprMenuId, oprMenuName, entityTmp.getStatus(), entityTmp.getAuditStatus(), String.valueOf(entityTmp.getId()), webNextUrl,entityTmp.getMchntNo(),null,entityTmp.getTermNo());
                    } else {
                        if(Common.isNotBlank(bizTmpService.getHisBizChangeData(oldEntity, entity))) {
                            bizHisService.saveBizHisNew(entity, oldEntity, user, org.getInstId(), bizOpr, lastUpddt
                                    , oprMenuId, oprMenuName, String.valueOf(entity.getId()), entity.getMchntNo(), null, entity.getTermNo());
                        }
//                bizTmpService.saveBizHis(entity, user, org.getInstId(), bizOpr, lastUpddt
//                        , oprMenuId, oprMenuName, String.valueOf(entity.getId()),entity.getMchntNo(),null,entity.getTermNo());
                    }
                }
            }

        } else if (PageOptFlagEnum.OPT_FLAG_CHECK.getType().equals(flag)) {
            String id = request.getParameter("id");
            if (Common.isBlank(id)) {
                 log.error("请求参数(id)异常!");
                return OperationResult.buildSuccessResult("f", result);
            }
            TermEquipment oldEntity = new TermEquipment();
            entity = this.getById(id);
            if (Common.isBlank(entity)) {
                 log.error("根据id没有找到指定数据！");
                return OperationResult.buildSuccessResult("f", result);
            }
            BeanUtils.copyProperties(entity,oldEntity);
            String opt = request.getParameter("opt");
            if (Common.isBlank(opt)) {
                 log.error("请求参数(opt)异常!");
                return OperationResult.buildSuccessResult("f", result);
            }
            String auditRefuseReason = request.getParameter("auditRefuseReason");
            if (BizDataAuditStatusEnum.AUDIT_STATUS_UPD_CHECKING.getType().equals(entity.getAuditStatus())) {
                if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                    if ("equout".equals(entity.getReserve6()) || "repout".equals(entity.getReserve6()) || "cheji".equals(entity.getReserve6())) {
                        deviceAction = new DeviceAction();
                        deviceAction.setId(DateUtils.formatDate(new Date(), "yyyyMMddHHmmssSSS"));
                        deviceAction.setDeviceNo(entity.getDeviceNo());
                        deviceAction.setBrandCd(entity.getBrandCode());
                        deviceAction.setModelCd(entity.getModelCode());
                        TermModelInfo termModel = termModelNewService.findTermModel(entity.getModelCode());
                        if (termModel == null) {
                            deviceAction.setModelName("");
                        } else {
                            deviceAction.setModelName(termModel.getModelName());
                        }
                        TermBrandInfo termBrand = termBrandNewService.findTermBrand(entity.getBrandCode());
                        if (termBrand == null) {
                            deviceAction.setBrandName("");
                        } else {
                            deviceAction.setBrandName(termBrand.getBrandName());
                        }
                        deviceAction.setSn(entity.getSn());
                        deviceAction.setOprId(user.getId());//登记操作人
                        deviceAction.setCrtOrgId(entity.getCrtOrgId());
                        deviceAction.setCreateTime(lastUpddt);
                        deviceAction.setLastUpdateTime(lastUpddt);
                        deviceAction.setLastOprId(user.getId());
                        deviceAction.setStatus("00");
                        deviceAction.setLeaseStartDate(entity.getLeaseStartDate());
                        deviceAction.setLeaseEndDate(entity.getLeaseEndDate());
                        deviceAction.setMchntNo(entity.getMchntNo());
                        if ("equout".equals(entity.getReserve6())) {//装机出库
                            entity.setStatus("14");
                            deviceAction.setAction("40");

                        } else if ("repout".equals(entity.getReserve6())) {//维修出库
                            entity.setStatus("13");
                            deviceAction.setAction("30");
                        } else if ("cheji".equals(entity.getReserve6())) {//撤机
                            entity.setStatus("15");
                            deviceAction.setAction("51");
                        }
                    } else if ("orgin".equals(entity.getReserve6()) || "repin".equals(entity.getReserve6()) || "chejiin".equals(entity.getReserve6())) {
                        if ("orgin".equals(entity.getReserve6())) {//返还入库
                            deviceAction = deviceActionService.query(entity.getDeviceNo(), "40");
                            deviceAction.setAction("42");//返还入库 修改状态
                        } else if ("repin".equals(entity.getReserve6())) {//修复入库
                            deviceAction = deviceActionService.query(entity.getDeviceNo(), "30");
                            deviceAction.setAction("31");//修复入库 修改状态

                        } else if ("chejiin".equals(entity.getReserve6())) {//撤机入库
                            deviceAction = deviceActionService.query(entity.getDeviceNo(), "51");
                            deviceAction.setAction("52");//修复入库 修改状态
                        }
                        deviceAction.setLastUpdateTime(lastUpddt);
                        deviceAction.setLastOprId(user.getId());
                        entity.setStatus("10");
                        entity.setMchntNo(null);
                        entity.setTermNo(null);
                    } else if ("warning".equals(entity.getReserve6()) || "sunhui".equals(entity.getReserve6()) || "loss".equals(entity.getReserve6()) || "give".equals(entity.getReserve6())) {
                        if ("warning".equals(entity.getReserve6())) {//报障
                            entity.setStatus("13");
                        } else if ("sunhui".equals(entity.getReserve6())) {//报损
                            entity.setStatus("12");
                        } else if ("loss".equals(entity.getReserve6())) {//丢失
                            entity.setStatus("16");
                        } else if ("give".equals(entity.getReserve6())) {//返还第三方
                            entity.setStatus("17");
                        }
                    }
                    entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_UPD_PASS.getType());
                } else {
                    entity.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_REJECT.getType());
                }
                bizOpr = BizOprEnum.OPR_UPDATE.getType();
            }
            if (deviceAction != null) {//终端设备在装机、换机、撤机、维修等操作过程中都会登记操作记录
                deviceAction.setInstId(entity.getInstId());
                deviceActionService.save(deviceAction);
            }
            entity.setAuditRefuseReason(auditRefuseReason);
            this.save(entity);
            if (PageOptFlagEnum.OPT_FLAG_PASS.getType().equals(opt)) {
                //审批通过将正式表更新到历史表
                if(Common.isNotBlank(bizTmpService.getHisBizChangeData(oldEntity, entity))) {
                    bizHisService.saveBizHisNew(entity, oldEntity, user, org.getInstId(), bizOpr, lastUpddt, oprMenuId, oprMenuName, String.valueOf(entity.getId()), entity.getMchntNo(), null, entity.getTermNo());
                }
//                bizTmpService.saveBizHis(entity, user, org.getInstId(), bizOpr, lastUpddt, oprMenuId, oprMenuName, String.valueOf(entity.getId()),entity.getMchntNo(),null,entity.getTermNo());
            }
            // 修改临时表的审核状态等字段
            bizTmpService.saveBizTmp(entity, user, org.getInstId(), bizOpr, lastUpddt, oprMenuId, oprMenuName, entity.getStatus(), entity.getAuditStatus(), String.valueOf(entity.getId()), webNextUrl,entity.getMchntNo(),null,entity.getTermNo());
        }
        return OperationResult.buildSuccessResult("s", result);
        //     return "s";
    }


    */
/**
     * 设备编号，格式为: DE+年月(6 位)+ 序列号(8位)
     *//*

    private String getAutoDeviceNo(String yearMonth) throws RedpException {
        String index = String.format("%08d", Integer.parseInt(upmpSysSeqService.updateParamValue("device", yearMonth, "20")));
        return BusinessContainer.DeviceNo_PREFIX + yearMonth + index;
    }


    @Transactional(rollbackFor = Exception.class)
    public void saveTermEquipments(List<TermEquipment> termEquipmentList, String oprMenuName, Long oprMenuId, String webNextUrl,HttpServletRequest request) throws RedpException {
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus arg0) {
                if(Common.isNotBlank(termEquipmentList)){
                    for(TermEquipment termEquipment : termEquipmentList){
                        */
/*设备编号*//*

                        String deviceNo = getAutoDeviceNo(DateUtils.formatDate(DateUtils.currentDate(), DateUtils.FORMAT_YYYYMM));
                        termEquipment.setDeviceNo(deviceNo);
                        */
/*入库方式*//*

                        termEquipment.setLibraryType("02");
                        */
/*设备状态*//*

                        termEquipment.setStatus("10");

                         UpmpUserInfoVO user = CommonUtils.getCurrentUser(upmpSysParamService);
                        SimpleDateFormat fmtdt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
                        UpmpOrganization org = organizationService.getOrgByCode(user.getOrgCode(), user.getInstId());
                        String lastUpddt = fmtdt.format(new Date());
                        termEquipment.setOprId(user.getId());
                        termEquipment.setCreateTime(lastUpddt);
                        if(user.isAdmin()){
                            String instId = request.getParameter("instId");
                            termEquipment.setInstId(instId);
                            UpmpOrganization headOrg = organizationService.getHeadOrgByCode(instId);
                            String crtOrgId = headOrg == null ? user.getOrgCode() : headOrg.getOrgCode();
                            termEquipment.setCrtOrgId(crtOrgId);
                        }else{
                            termEquipment.setInstId(user.getInstId());
                            termEquipment.setCrtOrgId(user.getOrgCode());
                        }
                        UpmpPrivilegeCheck privilegeCheck = authPrivilegeCheckService.getPrivilegeCheckByPrivilegeCode(user.getInstId(), oprMenuName + "终端库存管理:新增");
                        if (null != privilegeCheck && BusinessContainer.YES.equals(privilegeCheck.getChecked())) {
                            termEquipment.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_CHECKING.getType());
                            if (termEquipment.getDeviceNo() != null) {
                                save(termEquipment);
                            }
                            bizTmpService.saveBizTmp(termEquipment, user, org.getInstId(), BizOprEnum.OPR_ADD.getType(), lastUpddt
                                    , String.valueOf(oprMenuId), oprMenuName, termEquipment.getStatus(), termEquipment.getAuditStatus(), String.valueOf(termEquipment.getId()), webNextUrl,termEquipment.getMchntNo(),null,termEquipment.getTermNo());
                        } else {
                            termEquipment.setAuditStatus(BizDataAuditStatusEnum.AUDIT_STATUS_ADD_PASS.getType());
                            if (termEquipment.getDeviceNo() != null) {
                                save(termEquipment);
                            }
                            bizHisService.saveBizHisNew(termEquipment,termEquipment, user, org.getInstId(), BizOprEnum.OPR_ADD.getType(), lastUpddt, String.valueOf(oprMenuId), oprMenuName, String.valueOf(termEquipment.getId()),termEquipment.getMchntNo(),null,termEquipment.getTermNo());
                        }
                    }
                }
            }
        });
    }
}
*/
